/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modulo3;

import Modelo.DAL.ConexionBD;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONObject;
import Acceso.Security;

/**
 *
 * @author glpc
 */
public class listarExtintores extends org.apache.struts.action.Action {

  /* forward name="success" path="" */
  private static final String SUCCESS = "success";
  private static final String[] ALLOWED = {"Administrador","Gestionar Sistemas de Extincion"};

  /**
   * This is the action called from the Struts framework.
   * @param mapping The ActionMapping used to select this instance.
   * @param form The optional ActionForm bean for this request.
   * @param request The HTTP Request we are processing.
   * @param response The HTTP Response we are processing.
   * @throws java.lang.Exception
   * @return
   */
  @Override
  public ActionForward execute(ActionMapping mapping, ActionForm form,
          HttpServletRequest request, HttpServletResponse response)
          throws Exception {

      if (!Security.validateSession(request, ALLOWED)) {
            Security.privilegeError(response);
            return mapping.findForward("errorPriv");
        }

      response.setContentType("text/json");
    JSONObject data = new JSONObject();
    data.put("consultarExtintores", obtenerExtintores());

    PrintWriter pw = response.getWriter();
    pw.write(data.toString());
    pw.flush();
    pw.close();

    return mapping.findForward(SUCCESS);
  }

  public JSONObject obtenerExtintores() {
    ConexionBD conex = new ConexionBD();
    JSONObject Ext = new JSONObject();
    JSONObject id_nro_bien = new JSONObject();
    JSONObject sede = new JSONObject();
    JSONObject id_piso = new JSONObject();
    JSONObject nombre_edificio = new JSONObject();
    JSONObject tipo = new JSONObject();
    JSONObject capacidad = new JSONObject();
    JSONObject status = new JSONObject();
    JSONObject persona_contacto = new JSONObject();
    JSONObject referencia = new JSONObject();
    Statement st;
    ResultSet rs;
    try {
      st = conex.session.createStatement();

      rs = st.executeQuery("SELECT id_nro_bien FROM  extintor group  by id_nro_bien order by id_nro_bien desc");
      int i = 0;
      while (rs.next()) {
        id_nro_bien.put("" + i++, rs.getString(1));
      }

      rs = st.executeQuery("SELECT nombre_sede FROM  extintor group by nombre_sede order by nombre_sede desc");
      i = 0;
      while (rs.next()) {
        sede.put("" + i++, rs.getString(1));
      }

      rs = st.executeQuery("SELECT id_piso FROM  extintor group by id_piso order by id_piso desc");
      i = 0;
      while (rs.next()) {
        id_piso.put("" + i++, rs.getString(1));
      }

      rs = st.executeQuery("SELECT nombre_edificio FROM  extintor group by nombre_edificio order by nombre_edificio desc");
      i = 0;
      while (rs.next()) {
        nombre_edificio.put("" + i++, rs.getString(1));
      }

      rs = st.executeQuery("SELECT tipo FROM  extintor group by tipo order by tipo desc");
      i = 0;
      while (rs.next()) {
        tipo.put("" + i++, rs.getString(1));
      }

      rs = st.executeQuery("SELECT capacidad FROM  extintor group by capacidad order by capacidad desc");
      i = 0;
      while (rs.next()) {
        capacidad.put("" + i++, rs.getString(1));
      }

      rs = st.executeQuery("SELECT status FROM  extintor group by status order by status desc");
      i = 0;
      while (rs.next()) {
        status.put("" + i++, rs.getString(1));
      }

      rs = st.executeQuery("SELECT persona_contacto FROM  extintor group by persona_contacto order by persona_contacto desc");
      i = 0;
      while (rs.next()) {
        persona_contacto.put("" + i++, rs.getString(1));
      }

      rs = st.executeQuery("SELECT referencia FROM  extintor group by referencia order by referencia desc");
      i = 0;
      while (rs.next()) {
        referencia.put("" + i++, rs.getString(1));
      }
      Ext.put("id_nro_bien", id_nro_bien);
      Ext.put("sede", sede);
      Ext.put("id_piso", id_piso);
      Ext.put("nombre_edificio", nombre_edificio);
      Ext.put("tipo", tipo);
      Ext.put("capacidad", capacidad);
      Ext.put("status", status);
      Ext.put("persona_contacto", persona_contacto);
      Ext.put("referencia", referencia);

    } catch (Exception e) {
    }

    return Ext;
  }
}
